package com.dwxt.mall.admin.service;

import com.dwxt.common.base.YesOrNo;
import com.dwxt.mall.dao.ProductCategoryMapper;
import com.dwxt.mall.entity.ProductCategory;
import com.dwxt.mall.entity.ProductCategoryExample;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductAttrServiceAdmin {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductServiceAdmin productServiceAdmin;

    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        ProductCategoryExample example = new ProductCategoryExample();
        ProductCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andModuleIsNotNull().andModuleNotEqualTo("场馆培训券");
        if (productCategory != null) {
            if (StringUtils.isNotBlank(productCategory.getModule())) {
                criteria.andModuleEqualTo(productCategory.getModule());
            }
        }
        return productCategoryMapper.selectByExample(example);
    }

    public void createOrUpdate(ProductCategory productCategory) {
        if (productCategory.getId() == null) {
            productCategory.setIsShow(YesOrNo.YES);
            productCategoryMapper.insertSelective(productCategory);
        } else {
            ProductCategory category = productCategoryMapper.selectByPrimaryKey(productCategory.getId());
            if (!category.getModule().equalsIgnoreCase(productCategory.getModule())) {
                productServiceAdmin.changeModuleByCategoryId(productCategory);
            }
            productCategoryMapper.updateByPrimaryKeySelective(productCategory);
        }
    }

    public void delete(Integer id) {
        productCategoryMapper.deleteByPrimaryKey(id);
    }

    public List<ProductCategory> selectModuleProductCategory(String module) {
        ProductCategoryExample example = new ProductCategoryExample();
        if (StringUtils.isNotBlank(module)) {
            example.createCriteria().andModuleEqualTo(module);
        } else {
            example.createCriteria().andModuleIsNotNull();
        }
        return productCategoryMapper.selectByExample(example);
    }

    public void onOrDown(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if (productCategory.getIsOn() == YesOrNo.YES) {
            productCategory.setIsOn(YesOrNo.NO);
        } else {
            productCategory.setIsOn(YesOrNo.YES);
        }
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }
}
